var app = angular.module('myapp',['ngRoute','ngAnimate']);
	app.config(['$routeProvider',function($routeProvider){
		$routeProvider.when('/',{templateUrl:'html/home.html',controller:'home'})
		.when('/bourn',{templateUrl:'html/bourn.html',controller:'bourn'})
		.when('/store',{templateUrl:'html/store.html',controller:'store'})
		.when('/BBS',{templateUrl:'html/BBS.html',controller:'BBS'})
		.when('/mine',{templateUrl:'html/mine.html',controller:'mine'})
		.otherwise({redirectTo: '/'});
}]).controller('con',['$scope','$rootScope','$timeout','$location',function($scope,$rootScope,$timeout,$location){
	$scope.enrollpage = function(){
		$rootScope.enroll = true;
	}
	$scope.logmsgpage = function(tel){
		if(tel.length==0)  
        {  
           alert('请输入手机号码！');   
           return false;  
        }      
        if(tel.length!=11)  
        {  
            alert('请输入有效的手机号码！');  
            return false;  
        }  
        var myreg = /^1[3|4|5|8][0-9]\d{4,8}$/;
        if(!myreg.test(tel))  
        {  
            alert('请输入有效的手机号码！');  
            return false;  
        }
        console.log("验证码为666666");
        $rootScope.tel =tel;
	}
	$scope.verify = function(num){
		if(num != "666666"){
			alert("验证码错误");
			return false;
		}
		$rootScope.logmsg = true;
	}
	//页面后退
	$scope.loghide = function(){
		$rootScope.bool = false;
	}
	$scope.enrohide = function(){
		$rootScope.enroll = false;
	}
	$scope.logmhide = function(){
		$rootScope.logmsg = false;
	}
	//登录验证
	$scope.login = function(a,b,c){
		var ajax = new XMLHttpRequest();
		ajax.open('post','http://127.0.0.1/qy/login.php');
		ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax.send('username='+a+'&password='+b);
		ajax.onreadystatechange = function(){
			if(ajax.status == 200 && ajax.readyState == 4){
//				console.log(ajax.responseText);
				var obj = JSON.parse(ajax.responseText);
				//code为1，登陆成功
					if(obj.code == 1){
						//登录页面缩回
						$rootScope.bool = false;
						//跳转到‘我的’页面
						$location.path('/mine');
						$scope.$apply();
						//接收后台传过来的用户名数据
						$rootScope.name = obj.name;
						//显示欢迎界面
						$rootScope.hy = 1;
						if($rootScope.hy){
							$timeout(function(){
								$rootScope.hy = 0;
							},3000)
						}
						
					}else{
	//					code为0，登录失败
						alert(obj.msg);
					}		
			}
		}
	}
	//注册
	$scope.zc = function(a,b,c){
		if(a == "" || b == ""){
			alert("用户名或密码不得为空！")
		}else{
			var ajax = new XMLHttpRequest();
			ajax.open('post','http://127.0.0.1/qy/enroll.php');
			ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			ajax.send('newuser='+$rootScope.tel+'&newpass='+b+'&newname='+a);
			ajax.onreadystatechange = function(){
				if(ajax.status == 200 && ajax.readyState == 4){
					var obj = JSON.parse(ajax.responseText);
					console.log(obj);
					if(obj.code == 1){
						alert("注册成功！");
					}else{
						alert(obj.msg);
					}
					
				}
			}
		}
			
	}
	//动态fontSize
	document.documentElement.style.fontSize = innerWidth/16 + 'px';
		window.onresize = function(){
		document.documentElement.style.fontSize = innerWidth/16 + 'px';
	}
	//登录模块
	$scope.loginpage = function(){
		if($rootScope.name){
			
		}else{
			$rootScope.bool = true;
		}
	}
}])